package org.ipti.br.tag.bean;

import org.ipti.br.tag.bo.ActorBO;
import org.ipti.br.tag.bo.OrganizationBO;
import org.ipti.br.tag.bo.QuestionsBO;
import org.ipti.br.tag.bo.QuestionsPersonBO;
import org.ipti.br.tag.bo.UnityBO;
import org.ipti.br.tag.entity.Actor;
import org.ipti.br.tag.entity.Organization;
import org.ipti.br.tag.entity.Questions;
import org.ipti.br.tag.entity.QuestionsPerson;
import org.ipti.br.tag.entity.Unity;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import javax.inject.Inject;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;

@ManagedBean(name = "agreementBean")
@ViewScoped
public class AgreementBean extends AppBaseBean {
    
    @ManagedProperty("#{sessionBean}")
    private SessionBean sessionBean;
    @Inject
    private OrganizationBO organizationBO;
    @Inject
    private ActorBO actorBO;
    @Inject
    private UnityBO unityBO;
    @Inject
    private QuestionsBO questionsBO;
    @Inject
    private QuestionsPersonBO questionsPersonBO;
    private List<SelectItem> organizationList;
    private List<Actor> actorList;
    private List<SelectItem> unityList;
    private List<QuestionsPerson> questionsPersonList;
    private List<SelectItem> agreementList;
    private boolean success;
    private Long personageID;
    private Long orgFatherID;
    private Long unityFatherID;
    private Long unityID;
    private Long organizationID;
    private Long questionsID;
    
    public void initAgreement() {
    }
    
    public void redirectNewSearch() {
        redirect("/educational/questions/agreement.xhtml?organization=" + orgFatherID + "&personage=" + personageID + "&question=" + questionsID);
        success = false;
    }
    
    public void generateAgreementList() {
        actorList = new ArrayList<Actor>();
        if (unityID != 0) {
            actorList = getActorBO().actorsPerUnityPersonage(unityID, personageID);
            for (Actor act : actorList) {
                act.getPerson().setAgreement(questionsPersonBO.existsQuestionsPerson(questionsID, act.getPerson().getId()));
            }
        }
    }
    
    public void save() {
        for (Actor act : actorList) {
            if (act.getPerson().getAgreement() && !questionsPersonBO.existsQuestionsPerson(questionsID, act.getPerson().getId())){
                QuestionsPerson qp = new QuestionsPerson();
                qp.setPerson(act.getPerson());
                qp.setQuestions(questionsBO.find(questionsID));
                questionsPersonBO.create(qp);
            }else if (!act.getPerson().getAgreement() && questionsPersonBO.existsQuestionsPerson(questionsID, act.getPerson().getId())){
                questionsPersonBO.remove(questionsPersonBO.findQuestionsPerson(questionsID, act.getPerson().getId()));
            }
                
        }
        success = true;
    }
    
    public void loadOrganizationList() {
        organizationID = 0L;
        unityID = 0L;
        organizationList = new ArrayList<SelectItem>();
        unityList = new ArrayList<SelectItem>();
        actorList = new ArrayList<Actor>();
        Organization organization = getOrganizationBO().find(orgFatherID);
        organizationList.add(new SelectItem(0L, "Selecione uma Organização"));
        if (!getOrganizationBO().getOrganizationChildren(organization.getId()).isEmpty()) {
            getAllChildren(organization);
        }
    }
    
    public void loadUnityList() {
        unityID = 0L;
        unityList = new ArrayList<SelectItem>();
        actorList = new ArrayList<Actor>();
        if (organizationID != 0) {
            unityFatherID = getSessionBean().getLoggedActor().getUnity().getId();
            unityList.add(new SelectItem(0L, "Selecione uma Unidade"));
            for (Unity u : getUnityBO().getUnitiesPerOrganizationUnityFatherYear(organizationID, unityFatherID, Calendar.getInstance().get(Calendar.YEAR))) {
                unityList.add(new SelectItem(u.getId(), u.getName()));
            }
        }
    }
    
    
    public void getAllChildren(Organization organization) {
        List<Organization> organizationAux = getOrganizationBO().getOrganizationChildren(organization.getId());
        for (Organization org : organizationAux) {
            organizationList.add(new SelectItem(org.getId(), org.getName()));
        }
    }

    //Getters e Setters
    
    public OrganizationBO getOrganizationBO() {
        return organizationBO;
    }
    
    public void setOrganizationBO(OrganizationBO organizationBO) {
        this.organizationBO = organizationBO;
    }
    
    public Long getOrganizationID() {
        return organizationID;
    }
    
    public void setOrganizationID(Long organizationID) {
        this.organizationID = organizationID;
    }
    
    public List<SelectItem> getOrganizationList() {
        if (organizationList == null) {
            loadOrganizationList();
        }
        return organizationList;
    }
    
    public void setOrganizationList(List<SelectItem> organizationList) {
        this.organizationList = organizationList;
    }
    
    public SessionBean getSessionBean() {
        return sessionBean;
    }
    
    public void setSessionBean(SessionBean sessionBean) {
        this.sessionBean = sessionBean;
    }
    
    public Long getUnityFatherID() {
        return unityFatherID;
    }
    
    public void setUnityFatherID(Long unityFatherID) {
        this.unityFatherID = unityFatherID;
    }
    
    public Long getUnityID() {
        return unityID;
    }
    
    public void setUnityID(Long unityID) {
        this.unityID = unityID;
    }
    
    public Long getQuestionsID() {
        return questionsID;
    }
    
    public void setQuestionsID(Long questionsID) {
        this.questionsID = questionsID;
    }
    
    public List<SelectItem> getUnityList() {
        if (unityList == null) {
            unityList = new ArrayList<SelectItem>();
        }
        return unityList;
    }
    
    public void setUnityList(List<SelectItem> unityList) {
        this.unityList = unityList;
    }
    
    public UnityBO getUnityBO() {
        return unityBO;
    }
    
    public void setUnityBO(UnityBO unityBO) {
        this.unityBO = unityBO;
    }
    
    public ActorBO getActorBO() {
        return actorBO;
    }
    
    public void setActorBO(ActorBO actorBO) {
        this.actorBO = actorBO;
    }
    
    public List<Actor> getActorList() {
        return actorList;
    }
    
    public void setActorList(List<Actor> actorList) {
        this.actorList = actorList;
    }
    
    public Long getOrgFatherID() {
        return orgFatherID;
    }
    
    public void setOrgFatherID(Long orgFatherID) {
        this.orgFatherID = orgFatherID;
    }
    
    public boolean isSuccess() {
        return success;
    }
    
    public void setSuccess(boolean success) {
        this.success = success;
    }
    
    public QuestionsBO getQuestionsBO() {
        return questionsBO;
    }
    
    public void setQuestionsBO(QuestionsBO questionsBO) {
        this.questionsBO = questionsBO;
    }
    
    public Long getPersonageID() {
        return personageID;
    }
    
    public void setPersonageID(Long personageID) {
        this.personageID = personageID;
    }
    
    public QuestionsPersonBO getQuestionsPersonBO() {
        return questionsPersonBO;
    }
    
    public void setQuestionsPersonBO(QuestionsPersonBO questionsPersonBO) {
        this.questionsPersonBO = questionsPersonBO;
    }
    
    public List<QuestionsPerson> getQuestionsPersonList() {
        return questionsPersonList;
    }
    
    public void setQuestionsPersonList(List<QuestionsPerson> questionsPersonList) {
        this.questionsPersonList = questionsPersonList;
    }

    public List<SelectItem> getAgreementList() {
        agreementList = new ArrayList<SelectItem>();
        agreementList.add(new SelectItem(true, "Sim"));
        agreementList.add(new SelectItem(false, "Não"));
        return agreementList;
    }

    public void setAgreementList(List<SelectItem> agreementList) {
        this.agreementList = agreementList;
    }
    
    
}
